GBML manual / Grids

grid_copy

Makes a new grid that is a copy of source: the same width and height, every cell holding what it holds in source. Changing the copy never changes source, and the other way round. It goes on the right-hand side of the line that makes the variable, like grid_create, and it costs a whole second grid of memory.

Syntax

grid_copy(source) -> grid<T>

Arguments

NameType
sourcegrid

Returns

grid<T>

Description

Makes a new grid that is a copy of source: the same width and height, with every cell holding what it holds in source. Changing the copy never changes source, and changing source never changes the copy.

var backup = grid_copy(source_grid);

The copy is the same size

The copy is exactly as wide and as tall as source. A grid made with grid_create(8, 6, 0) makes a copy that is 8 across and 6 down, so [# x, y] works on it straight away, and grid_width and grid_height give the same two numbers for both.

A copy takes its size from source, so there is no size for you to write. A copy given a smaller size of its own could not hold everything the original holds, so putting a size beside one (var backup: grid<int, 2, 2> = grid_copy(source);, or a /// @capacity tag) is refused when the project is checked.

What it costs

A copy is a second whole grid. The Game Boy Advance has no spare memory to hand out while a game runs, so the room for the copy is set aside when the game is built, in full, whether or not it ever fills up. A 20-by-20 grid of numbers makes a copy that takes room for 400 numbers. Two copies take the room of two. A copy in an object's variable keeps its room for as long as the object exists, even when nothing uses it any more.

Making the copy takes time as well. It walks through every cell that source holds and copies each one, so the time grows with how many there are. A few cells copied when something happens - a button press, a new level - is nothing. Hundreds of cells copied twice in every frame is a real part of the frame spent copying, and it adds up on a real cartridge. Copy when you need to, not every frame.

Where the room comes from depends on where the variable lives:

Where a copy can go

A copy is a new container, and its size belongs to the variable that holds it - so it goes in the same two places grid_create does, and nowhere else:

Not inside another call or a sum, not on a line by itself, not in a later event such as Step, and not over a variable that already exists - that variable already has a size, and a copy of a different size cannot be put in it. To fill a grid from another one later in the game, set its cells one at a time with two loops, one inside the other.

What it can copy

source has to be a variable written on its own, like grid_copy(scores), whose size the compiler knows: one made in the same event, or an object variable made in Create. Anything else is refused with a plain message:

Giving it the wrong kind of container is refused too: grid_copy copies a grid and nothing else.

grid_width and grid_height tell you how big the copy is.

Example

var board = grid_create(8, 6, 0);
board[# 2, 3] = 5;
var saved = grid_copy(board);   // 8 across and 6 down, with the 5 in it
saved[# 2, 3] = 0;              // changes only the copy: board[# 2, 3] is still 5

On the GBA

A copy of the fixed-size vector of W*H cells - the new grid has the same width and height as source, so [# x, y] works on it straight away, and grid_width/grid_height give the same numbers. A copy is a second whole container, so it costs a second container's worth of memory: as many places as the original has room for (its capacity, not just how many it holds right now), and copying is O(n) in how many items the original holds. The Game Boy Advance has no heap, so the copy's size is fixed when the game is built - it is the original's own declared size, which is why the original has to be a variable whose size is known here (otherwise it is refused: a script's parameter could be any size). Like a creation call it has to be the whole right-hand side of the line that makes a variable, and for an object's variable that line is in its Create event, where every instance of the object gets its own copy of the memory. A copy is always the same size as the original, so a size written beside it is refused.

See also: grid_creategrid_widthgrid_heightgrid_fillGridsarray_copyData containersgrid_sortgrid_shufflegrid_filter